<template>
  <van-popup
    round
    v-model="boxshow"
    :position="confirmOrderPosition"
    @close="closePop"
    class="popup-cont confirm-order-custom"
    :get-container="getContainer"
    :style="{
      height: '80%',
      fontSize: '0.32rem',
      backgroundColor: '#F5F5F5',
      borderRadius:'16px',
      overflowY: necessaryDataLoaded ?'auto !important' : 'hidden'
    }"
  >
    <!-- 顶部 -->
    <div class="popup-header">
      <div class="popup-header-title">{{ t('confirmOrder_confirm_order') }}</div>
      <i
        class="van-icon van-icon-cross van-action-sheet__close"
        @click="closePop"
        v-show="sheetInfo.showClose"
      ></i>
      <i
        class="van-icon van-icon-arrow-left van-action-sheet__left"
        @click="backClick"
        v-show="sheetInfo.showBack"
      ></i>
    </div>
    <div class="content-box">
      <Skeleton skeletonType="order" show_bottom v-show="!necessaryDataLoaded"></Skeleton>
      <!-- 内容 -->
      <Order
        ref="confirmOrderRef"
        v-if="boxshow && isNewOrder"
        v-show="necessaryDataLoaded"
        :baseInfoQuery.sync="baseInfoQuery"
        :isCart.sync="isCart"
        :isTeam.sync="isTeam"
        :cartGoodsList.sync="cartGoodsList"
        :boxshow.sync="boxshow"
        :sheetInfo.sync="sheetInfo"
        :goodsMsg="goodsMsg"
        :useScatter="useScatter"
        :aliveInfo="aliveInfo"
        :getContainer="getContainer"
        :miniprogramSdkPaySetting="miniprogramSdkPaySetting"
        @dataLoaded="()=>{necessaryDataLoaded=true}"
        @changeSheetInfo="changeSheetInfo"
        @successCallback="successCallback"
        @failCallback="failCallback"
        @refresh-exceed="closePop"
      ></Order>
      <OldOrder
        ref="confirmOrderRef"
        v-else-if="boxshow"
        v-show="necessaryDataLoaded"
        :baseInfoQuery.sync="baseInfoQuery"
        :isCart.sync="isCart"
        :isTeam.sync="isTeam"
        :cartGoodsList.sync="cartGoodsList"
        :boxshow.sync="boxshow"
        :sheetInfo.sync="sheetInfo"
        :goodsMsg="goodsMsg"
        :useScatter="useScatter"
        :aliveInfo="aliveInfo"
        @dataLoaded="()=>{necessaryDataLoaded=true}"
        @changeSheetInfo="changeSheetInfo"
        @successCallback="successCallback"
        @failCallback="failCallback"
        @refresh-exceed="closePop"
      ></OldOrder>
    </div>
  </van-popup>
</template>
<script>

import locale from "../../../../mixins/locale";
import Order from "./confirmOrder.vue";
import OldOrder from "./oldConfirmOrder.vue";
import Skeleton from "../Skeleton/index.vue";
import * as api from "./api";

export default {
  name: "ConfirmOrder",
  mixins: [locale],
  components: {
    Skeleton,
    Order,
    OldOrder
  },
  data() {
    return {
      sheetInfo: {
        // 确认订单弹框配置信息
        title: this.t('confirmOrder_confirm_order'),
        showClose: true,
        showBack: false,
      },
      necessaryDataLoaded: false,
      isNewOrder: false,
    }
  },
  props: {
    aliveInfo: {
      type: Object,
      default: {},
    },
    boxshow: {
      type: Boolean,
      default: false,
    },
    baseInfoQuery: {
      type: Object,
      default: () => {
      },
    },
    isCart: {
      type: Number,
      default: 0,
    },
    isTeam: {
      type: Boolean,
      default: false,
    },
    cartGoodsList: {
      type: Array,
      default: () => [],
    },
    popupRoundStyle: {
      type: Object,
      default: () => {
        return {
          height: "80%",
        };
      },
    },
    goodsMsg: { // 从商品卡片进入的标识
      type: Boolean,
      default: false,
    },
    useScatter: {
      type: Boolean,
      default: false
    },
    // appen的dom
    getContainer: {
      type: String,
      default: 'body'
    },
    // 确认订单弹窗位置
    confirmOrderPosition: {
      type: String,
      default: 'bottom'
    },
    miniprogramSdkPaySetting: {
        type: Object,
        default: () => ({}),
    },
  },
  methods: {
    // 关闭弹框
    closePop() {
      this.$emit("update:boxshow", false);
      this.necessaryDataLoaded = false
    },
    // 返回箭头点击事件
    backClick() {
      // TODO
      // this.$refs.confirmOrderRef.backClick();
    },
    changeSheetInfo(val) {
      this.sheetInfo = {
        ...this.sheetInfo,
        ...val,
      };
    },
    // 支付成功
    successCallback(payResult) {
      this.$emit("successCallback", payResult);
    },
    // 支付失败
    failCallback() {
      this.$emit("failCallback");
    },
    // 新老订单灰度
    getOrderGray(){
      let params = {
        gray_names: ["order_confirm_detail"]
      }
      api.newOrderGrayControl(params).then((res) => {
        if (res.code === 0) {
          this.isNewOrder = Boolean(res.data.order_confirm_detail)
        }else {
          this.isNewOrder = false
        }
      });
    },
  },
  watch: {
    boxshow: {
      handler: function (value) {
        if (!value) {
          this.sheetInfo = {
            title: this.t('confirmOrder_confirm_order'),
            showClose: true,
            showBack: false,
          };
        }else{
          this.getOrderGray()
        }
      },
    },
  },
  mounted(){
    this.getOrderGray()
  }
}
</script>
<style scoped lang="scss">
@import "../../assets/styles/global.scss";
@import "../../assets/styles/common.scss";
.van-action-sheet__left {
  position: absolute;
  top: 0;
  left: 0;
  padding: 0 16px;
  color: #c8c9cc;
  font-size: 22px;
  line-height: inherit;
}

.coupon-integral-wrapper {
  margin-bottom: 0.16rem;
}

.popup-cont {
  display: flex;
  flex-direction: column;

  .popup-header {
    width: 100%;
    max-width: 480px;
    position: fixed;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.24rem 0;
    background: rgba(255, 255, 255, 1);
    border-radius: 0.32rem 0.32rem 0 0;
    min-height: 0.48rem;

    .popup-header-title {
      font-weight: 600;
      line-height: 0.48rem;
    }

    .van-action-sheet__left {
      position: absolute;
      top: 0.32rem;
      left: 0;
      color: #aaaaaa;
      font-size: 0.32rem;
    }

    .van-action-sheet__close {
      position: absolute;
      top: 0.32rem;
      right: 0;
      color: #aaaaaa;
      font-size: 0.32rem;
    }
  }
}


.content-box {
  flex: 1;
  box-sizing: border-box;
  padding-top: 0.96rem;
  padding-bottom: 1.62rem;
  border-radius: 16px 16px 0 0;
}

</style>
